package com.module.listen;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Mr.Zhong
 * @create: 2018-07-03 16:08
 * @description:
 **/
public class SessionListener implements HttpSessionAttributeListener {

    /**
     * 定义监听的session属性名.
     */
    public final static String LISTENER_NAME = "_login";

    /**
     * 定义存储客户登录session的集合.
     */
    private static List sessions = new ArrayList();


    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (LISTENER_NAME.equals(httpSessionBindingEvent.getName())) {
            sessions.add(httpSessionBindingEvent.getValue());
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        if (LISTENER_NAME.equals(httpSessionBindingEvent.getName())) {
            sessions.remove(httpSessionBindingEvent.getValue());
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {

    }

    /**
     * 返回客户登录session的集合.
     *
     * @return
     */
    public static List getSessions() {
        return sessions;
    }
}
